package com.itheima.service.impl;

import com.itheima.mapper.AccountMapper;
import com.itheima.pojo.Account;
import com.itheima.service.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author zhouxiangyang
 * @Date 2022/4/6 21:05
 */
@Service //将Bean添加到spring容器中
@Slf4j //用于打印日志信息
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;
    /**
     * 实现转账业务:
     * 要求:转出账户金额充足才可以进行转账
     * @param fromId 转出账户id
     * @param toId 转入账户id
     * @param amount 转账金额
     */
    @Override
    public void transfer(int fromId, int toId, double amount) {
        //1 根据fromId查询转出账户信息，判断账户余额是否充足
        Account byId = accountMapper.findById(fromId);
        if (byId.getAmount()>amount){
            //2 转出账户余额充足，开始进行转换
            accountMapper.updateAmount2(fromId, amount);
            int x=1/0;
            accountMapper.updateAmount1(toId, amount);
        }else {
            System.out.println("余额不足");
        }
    }
}
